// pages/shapeSelect/shapeSelect.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 0,
    saveStatus: false,
    list: [],
    selectImg: '',
    selectId: 0
  },
  save () {
    this.setData({
      saveStatus: true
    })
    getApp().https.ajax({
      method: 'get',
      url: '/member/createUserShape',
      data: { shapeId: this.data.selectId, type: this.data.type + 1 },
      loading: true
    }).then(() => {
      wx.showToast({
        title: '保存成功!',
        icon: 'none'
      })
      this.setData({
        saveStatus: false
      })
    })
  },
  switch (e) {
    let id = e.currentTarget.dataset.id
    let index = e.currentTarget.dataset.index
    this.setData({
      selectImg: this.data.list[index].img1,
      selectId: id
    })
  },
  getList () {
    let url = this.data.type === 0 ? '/shape/getHandShapeList' : '/shape/getFaceShapeList'
    getApp().https.ajax({
      method: 'get',
      url: url,
      loading: true
    }).then(({ rows }) => {
      this.setData({
        list: rows,
        selectImg: rows[0].img1,
        selectId: rows[0].id
      }, () => {
        this.getDetail()
      })
    })
  },
  getDetail () {
    let url = this.data.type === 0 ? '/shape/selectHandShape' : '/shape/selectFaceShape'
    getApp().https.ajax({
      method: 'post',
      url: url,
      loading: true
    }).then(({ data }) => {
      this.setData({
        selectImg: data.img1 ? data.img1 : this.data.selectImg,
        selectId: data.id ? data.id : this.data.selectId
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      type: parseInt(options.type) || 0
    }, () => {
      this.getList()
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})